package net.funwoo.pandago.ui.main;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageButton;

import net.funwoo.pandago.R;
import net.funwoo.pandago.ui.BaseActivity;

import java.util.List;

public class MainActivity extends BaseActivity {

    private ImageButton mNavHomeBtn;
    private ImageButton mNavMsgBtn;
    private ImageButton mNavPersonBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main, true);
    }

    @Override
    protected void setupToolbar(int themeColor) {
        super.setupToolbar(themeColor);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    }

    @Override
    protected void initView() {
        mNavHomeBtn = (ImageButton) findViewById(R.id.main_nav_home);
        mNavMsgBtn = (ImageButton) findViewById(R.id.main_nav_msg);
        mNavPersonBtn = (ImageButton) findViewById(R.id.main_nav_person);

        // TODO LOAD default fragments


        onHomeBtnClick(mNavHomeBtn);
    }

    public void onHomeBtnClick(View view) {
        mNavHomeBtn.setSelected(false);
        mNavMsgBtn.setSelected(false);
        mNavPersonBtn.setSelected(false);
        view.setSelected(true);
        int id = view.getId();

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        // hide all fragments first
        List<Fragment> fragments = fragmentManager.getFragments();
        if (fragments != null && fragments.size() > 0) {
            for (Fragment fragment : fragments) {
                if (transaction != null && fragment.isAdded()) {
                    transaction.hide(fragment);
                }
            }
        }

        Fragment fragment;
        String name;
        switch (id) {
            case R.id.main_nav_home:
                name = HomeFragment.TAG;
                fragment = fragmentManager.findFragmentByTag(name);
                if (fragment == null) {
                    fragment = new HomeFragment();
                    transaction.add(R.id.main_container, fragment, name);
                }
                break;
            case R.id.main_nav_msg:
                name = MessageFragment.TAG;
                fragment = fragmentManager.findFragmentByTag(name);
                if (fragment == null) {
                    fragment = new MessageFragment();
                    transaction.add(R.id.main_container, fragment, name);
                }
                break;
            case R.id.main_nav_person:
                name = PersonFragment.TAG;
                fragment = fragmentManager.findFragmentByTag(name);
                if (fragment == null) {
                    fragment = new PersonFragment();
                    transaction.add(R.id.main_container, fragment, name);
                }
                break;
            default:
                name = HomeFragment.TAG;
                fragment = fragmentManager.findFragmentByTag(name);
                if (fragment == null) {
                    fragment = new HomeFragment();
                    transaction.add(R.id.main_container, fragment, name);

                }
                break;
        }

        // TODO change title
        transaction.show(fragment);
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        transaction.commit();

        if (name.equals(HomeFragment.TAG)) {
            setTitle(getString(R.string.title_home));
        } else if (name.equals(MessageFragment.TAG)) {
            setTitle(getString(R.string.title_message));
        } else if (name.equals(PersonFragment.TAG)) {
            setTitle(getString(R.string.title_person));
        }
    }
}
